package flc.ast.fragment;

import android.os.Bundle;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.blankj.utilcode.util.ClipboardUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.footer.ClassicsFooter;
import com.scwang.smartrefresh.layout.header.ClassicsHeader;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;

import java.util.List;

import flc.ast.AppConfig;
import flc.ast.R;
import flc.ast.activity.TextActivity;
import flc.ast.adapter.TextAdapter;
import flc.ast.databinding.FragmentTabBinding;
import flc.ast.utils.Content;
import stark.common.api.StkApi;
import stark.common.api.StkResApi;
import stark.common.base.IReqRetCallback;
import stark.common.basic.base.BaseNoModelFragment;
import stark.common.basic.utils.IntentUtil;
import stark.common.bean.StkResBean;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link TabFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class TabFragment extends BaseNoModelFragment<FragmentTabBinding> {

    private TextAdapter textAdapter;
    private int page = 1;
    private int refreshTime = 200;

    public static TabFragment newInstance(String hashId) {
        Bundle args = new Bundle();
        args.putSerializable(Content.Url, hashId);
        TabFragment fragment = new TabFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    protected int onCreate() {
        return R.layout.fragment_tab;
    }

    @Override
    protected void initView() {
        //tab
        mDataBinding.rvTabList.setLayoutManager(new LinearLayoutManager(mContext));
        textAdapter = new TextAdapter();
        mDataBinding.rvTabList.setAdapter(textAdapter);
        textAdapter.addChildClickViewIds(R.id.ivTextItemCopy);
        textAdapter.addChildClickViewIds(R.id.ivTextItemShare);
        textAdapter.setOnItemChildClickListener(this);
        textAdapter.setOnItemClickListener(this);
    }

    @Override
    protected void onItemClickCallback(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        StkResBean bean = (StkResBean) adapter.getItem(position);
        TextActivity.stkResBean = bean;
        startActivity(TextActivity.class);
    }

    @Override
    public void onItemChildClick(@NonNull BaseQuickAdapter adapter, @NonNull View view, int position) {
        switch (view.getId()) {
            case R.id.ivTextItemCopy://复制
                ClipboardUtils.copyText(textAdapter.getItem(position).getDesc());
                ToastUtils.showShort(getString(R.string.copy_success_tips));
                break;
            case R.id.ivTextItemShare://分享
                IntentUtil.shareText(mContext, textAdapter.getItem(position).getDesc());
                break;
        }
    }

    @Override
    protected void initData() {
        String hashId = getArguments().getString(Content.Url);
        getData(hashId);
        mDataBinding.refreshTabItem.setRefreshHeader(new ClassicsHeader(mContext));
        mDataBinding.refreshTabItem.setRefreshFooter(new ClassicsFooter(mContext));
        mDataBinding.refreshTabItem.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                page++;
                getData(hashId);
                mDataBinding.refreshTabItem.finishLoadMore(refreshTime);
            }

            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                page = 1;
                getData(hashId);
                mDataBinding.refreshTabItem.finishRefresh(refreshTime);
            }
        });
    }

    private void getData(String hashId) {
        //获取数据
        StkResApi.getTagResourceList(null, AppConfig.URL_PREFIX + hashId, StkApi.createParamMap(0, 12), false,
                new IReqRetCallback<List<StkResBean>>() {
                    @Override
                    public void onResult(boolean success, String msg, @Nullable List<StkResBean> result) {
                        if (success) {
                            if (page == 1) {
                                textAdapter.setList(result);
                            } else {
                                textAdapter.addData(result);
                            }
                        }
                    }
                });
    }

}